package com.example.controller;

import com.example.entity.LeaveRequest;
import com.example.service.FlowService;
import org.flowable.task.api.Task;
import org.flowable.task.api.history.HistoricTaskInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 工作流控制器
 */
@RestController
@RequestMapping("/api/flow")
public class FlowController {
    
    @Autowired
    private FlowService flowService;
    
    /**
     * 启动请假流程
     */
    @PostMapping("/leave/start")
    public Map<String, String> startLeaveProcess(@RequestBody LeaveRequest leaveRequest) {
        String processInstanceId = flowService.startLeaveProcess(leaveRequest);
        Map<String, String> result = new HashMap<>();
        result.put("processInstanceId", processInstanceId);
        result.put("message", "请假流程已启动");
        return result;
    }
    
    /**
     * 查询待办任务
     */
    @GetMapping("/tasks/{assignee}")
    public List<Task> getTasksByAssignee(@PathVariable String assignee) {
        return flowService.findTasksByAssignee(assignee);
    }
    
    /**
     * 完成任务
     */
    @PostMapping("/tasks/{taskId}/complete")
    public Map<String, String> completeTask(@PathVariable String taskId, @RequestBody Map<String, Object> variables) {
        flowService.completeTask(taskId, variables);
        Map<String, String> result = new HashMap<>();
        result.put("message", "任务已完成");
        return result;
    }
    
    /**
     * 查询流程历史
     */
    @GetMapping("/history/{processInstanceId}")
    public List<HistoricTaskInstance> getProcessHistory(@PathVariable String processInstanceId) {
        return flowService.getProcessHistory(processInstanceId);
    }
    
    /**
     * 查询流程变量
     */
    @GetMapping("/variables/{executionId}")
    public Map<String, Object> getProcessVariables(@PathVariable String executionId) {
        return flowService.getProcessVariables(executionId);
    }
}